﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace MagicFight
{
    public class Consecration : Magic
    {
        Dictionary<Character, double> targets = new Dictionary<Character, double>(); // alvos que foram afetados pelo raio
        int range = Def.ConsecrationDef.Range;
        int damage = Def.ConsecrationDef.Damage;
        int healingAmount = Def.ConsecrationDef.HealingAmount;
        int duration = Def.ConsecrationDef.Duration;
        Vector2 position;
        int interval = Def.ConsecrationDef.Interval;

        GreenGlow gg = null;

        public Consecration(Character caster)
            : base(caster, Def.ConsecrationDef.N)
        {
            position = caster.position;
        }

        public override void Effect(GameTime gameTime)
        {
            if (gg == null)
            {
                gg = new GreenGlow(position, range);
                gg.timer = duration;
                ps.effectsManager.Add(gg);
            }
            if (!alive) return;

            if (lifeTime > duration) { alive = false; return; }

            CheckCollision(gameTime);
        }

        private void CheckCollision(GameTime gameTime)
        {
            foreach (Character c in ps.characters)
            {
                if (c == null) continue;
                if (!c.alive) continue;

                if (Collision(c))
                {
                    
                    if (targets.ContainsKey(c)) targets[c] += gameTime.ElapsedGameTime.TotalMilliseconds;
                    else targets.Add(c, gameTime.ElapsedGameTime.TotalMilliseconds);
                    if (targets[c] > interval)
                    {
                        targets[c] -= interval;
                        if (c == caster)
                        {
                            int d = c.GainHP(healingAmount);
                            if (d > 0) ps.effectsManager.Add(new FloatingText(c.position, d.ToString(), Color.Green));
                        }
                        else
                        {
                            int d = c.TakeDamage(damage, caster);
                            if (d > 0) ps.effectsManager.Add(new FloatingText(c.position, d.ToString(), Color.Red));
                        }
                    }
                }
            }
        }

        private bool Collision(Character c)
        {
            return Vector2.Distance(position, c.position) < (this.range + c.radius);
        }
    }
}
